package com.reebake.ideal.user.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reebake.ideal.holder.UserContextHolder;
import com.reebake.ideal.user.entity.UserCredential;
import com.reebake.ideal.user.service.UserAuthService;
import com.reebake.ideal.user.service.UserCredentialService;
import com.reebake.ideal.user.vo.UserCredentialConfigVO;
import com.reebake.ideal.user.vo.UserCredentialRegisterVO;
import com.reebake.ideal.user.vo.UserCredentialVO;
import com.reebake.ideal.user.vo.UserRegisterVO;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/credential")
@RequiredArgsConstructor
public class UserCredentialPlusController {
    private final UserCredentialService userCredentialService;
    private final UserAuthService userAuthService;

    @GetMapping("/query-by-user-id/{userId}")
    public UserCredentialVO queryById(@PathVariable String userId){
        LambdaQueryWrapper<UserCredential> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCredential::getUserId, userId);

        UserCredential userCredential = userCredentialService.getOne(queryWrapper);
        return BeanUtil.copyProperties(userCredential, UserCredentialVO.class);
    }

    @PostMapping("/username")
    public void changeUsername(@RequestBody UserCredentialRegisterVO userCredentialRegisterVO) {
        String userId = UserContextHolder.getCurrentUserId();
        String password = userCredentialRegisterVO.getPassword();
        String newUsername = userCredentialRegisterVO.getUsername();
        userAuthService.changeUsername(userId, password, newUsername);
    }

    @PostMapping("credential/password")
    public void changePassword(@RequestBody UserCredentialRegisterVO userCredentialRegisterVO) {
        String userId = UserContextHolder.getCurrentUserId();
        String password = userCredentialRegisterVO.getPassword();
        String newPassword = userCredentialRegisterVO.getNewPassword();
        userAuthService.changePassword(userId, password, newPassword);
    }

    @PostMapping("credential/phone-number")
    public void changePhoneNumber(@RequestBody UserCredentialRegisterVO userCredentialRegisterVO) {
        String userId = UserContextHolder.getCurrentUserId();
        String verifyCode = userCredentialRegisterVO.getVerifyCode();
        String phoneNumber = userCredentialRegisterVO.getPhoneNumber();
        String newVerifyCode = userCredentialRegisterVO.getNewVerifyCode();
        userAuthService.changePhoneNumber(userId, verifyCode, phoneNumber, newVerifyCode);
    }

    @PostMapping("/register/{userId}")
    public Boolean register(@PathVariable String userId, @RequestBody @Valid UserRegisterVO userRegisterVO) {
        if(StrUtil.isNotBlank(userRegisterVO.getPhoneNumber())) {
            userAuthService.saveCredentialByPhoneNumber(userId, userRegisterVO.getPhoneNumber(), userRegisterVO.getVerifyCode());
        }else if(StrUtil.isNotBlank(userRegisterVO.getUsername())) {
            userAuthService.saveCredentialByUsername(userId, userRegisterVO.getUsername(), userRegisterVO.getPassword());
        }else if(StrUtil.isNotBlank(userRegisterVO.getEmail())) {
            userAuthService.saveCredentialByEmail(userId, userRegisterVO.getEmail(), userRegisterVO.getVerifyCode());
        }else {
            throw new UnsupportedOperationException();
        }
        return true;
    }

    @PostMapping("/config/{userId}")
    public void config(@PathVariable String userId, @RequestBody @Valid UserCredentialConfigVO userCredentialConfigVO){
        UserCredential userCredential = BeanUtil.copyProperties(userCredentialConfigVO, UserCredential.class);
        LambdaQueryWrapper<UserCredential> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCredential::getUserId, userId);

        if(userCredentialService.exists(queryWrapper)){
            userCredentialService.update(userCredential, queryWrapper);
        }else {
            userCredential.setUserId(userId);
            userCredentialService.save(userCredential);
        }
    }
}
